package com.snowflakes.gruntr;

import java.io.ByteArrayOutputStream;
import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;







import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseUser;


public class MakePost extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.make_post);

		final Context context = getApplicationContext();


		Button submitPost = (Button) findViewById(R.id.bSubmitPost);
		Button photo = (Button) findViewById(R.id.bPhoto);
		final ImageView imageview = (ImageView) findViewById(R.id.ivPostImage);
		
		final CharSequence[] items={"Camera", "Gallery"};

		submitPost.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ParseUser user = ParseUser.getCurrentUser();
				ParseObject Post = new ParseObject("Post");
				
				imageview.setDrawingCacheEnabled(true);
				imageview.buildDrawingCache();
				Bitmap bm = imageview.getDrawingCache();
				
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
				byte[] byteArray = stream.toByteArray();
				
				ParseFile imageFile = new ParseFile("image.jpg", byteArray);
				imageFile.saveInBackground();
				
				Post.put("Image", imageFile);
				Post.put("CreatedBy", user);
				Post.put("Grunts", 0);
				Post.put("Name", user.get("name"));

				Post.saveInBackground();

				Toast.makeText(context, "Post Successful", Toast.LENGTH_LONG)
						.show();

				finish();
			}
		});
		
		photo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder3 = new AlertDialog.Builder(
						MakePost.this);
				builder3.setTitle("Select").setItems(items,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								if(which == 0){
									Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
									startActivityForResult(takePicture, 0);
								}
								else{
									Intent pickPhoto = new Intent(Intent.ACTION_PICK,
									           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
									pickPhoto.setType("image/*");
									startActivityForResult(pickPhoto , 1);
								}
							}
						});
				builder3.show();
			}
		});

		

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		ImageView imageview = (ImageView) findViewById(R.id.ivPostImage);
		
		if(data == null){
			//imageReady = false;
		}
		
		switch (requestCode) {
		case 0:
			if (resultCode == RESULT_OK) {
				//Uri selectedImage = imageReturnedIntent.getData();
				//imageview.setImageURI(selectedImage);
				
				Bundle extras = data.getExtras();
				Bitmap imageBitmap = (Bitmap) extras.get("data");

				imageview.setImageBitmap(imageBitmap);
				//imageReady = true;
			}

			break;
		case 1:
			if (resultCode == RESULT_OK) {
				Intent imageReturnedIntent;
	            Uri selectedImage = data.getData();
	            String[] filePathColumn = { MediaColumns.DATA };
	            Cursor cursor = getContentResolver().query(selectedImage,
	            filePathColumn, null, null, null);
	            cursor.moveToFirst();
	            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	            String filePath = cursor.getString(columnIndex);
	            File mFile = new File(filePath);
	            cursor.close();
	            BitmapFactory.Options options = new BitmapFactory.Options();
	             options.inSampleSize = 2;
	           Bitmap bp = BitmapFactory.decodeFile(filePath, options);
	           imageview.setImageBitmap(bp);
			}
			break;
		}
	}

}
